/*
 * @Author: shiguang
 * @Date: 2023-04-28 11:51:00
 * @LastEditors: yusha
 * @LastEditTime: 2023-08-25 13:52:44
 * @Description: interceptors
 */

import { message } from 'antd';
import { api, apiInstanceList } from '@/service';
// import { getRouter } from '../router/createRouter';
import { getRouter } from '@/App';
export interface User {
  loginInfo: {
    userId: string;
    username: string;
  };
  userInfo: {};
  auth: never[];
  token: string;
}
const getUserInLocal = () => {
    const usrStr = window.localStorage.getItem('user');
    if (!usrStr) return;
    try {
        const user = JSON.parse(usrStr).user as User;
        return user;
    } catch (e) {
        return undefined;
    }
};


export enum ENUM_RES_CODE{
    /** token 过期 */
    EXPIRE = '10000000',
}

// export const MAP_RES_CODE = new Map([
//     [ENUM_RES_CODE.EXPIRE, {
//         handle: () => {
//             window.location.pathname = '/test/summary';
//         }
//     }],
// ]);

apiInstanceList.forEach(item => {
    // 请求拦截
    item.instance.instance.interceptors.request.use(function (config) {
        config.headers = config.headers ?? {};
        const isPost = ['POST', 'post'].includes(config.method!);
        // const token = getUserInLocal()?.token;
        // if (token) {
        //     config.headers['X-AuthToken'] = token;
        // }
        if (isPost) {
            if (!config.data && config.params) {
                config.data = config.params;
            }
            config.params = {};
            config.headers['Content-Type'] = 'application/json';
        }
        return config;
    });

    // 返回拦截
    // item.instance.instance.interceptors.response.use(function (response) {
    //     const code = response.data?.code;
    //     console.log('99999999999999999999999',response);
        
    //     // const codeConfig = MAP_RES_CODE.get(code);
    //     // if(codeConfig){
    //     //     codeConfig.handle();
    //     //     return Promise.reject(response.data.msg);
    //     // }
    //     if (response.data?.code === 200) return response;
    //     message.error('请求错误：' + response.data.msg);
    //     return Promise.reject(response.data.msg);
    // }, function (error) {
    //     message.error('请求错误：' + String(error));
    //     return Promise.reject(error);
    // });
});
export const request = api;